package com.leyou.sms.utils;

import com.leyou.sms.config.SmsProperties;
import com.leyou.sms.constants.SmsConstants;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Random;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Slf4j
@Component
public class SmsUtils {
    @Autowired
    private SmsClient smsClient;
    @Autowired
    private SmsProperties smsProperties;

    /**
     * 发送验证码
     * @param phone 电话号码
     * @param code 验证码
     */
    public void sendVerifyCode(String phone,String code){
        SendSmsRequest req = new SendSmsRequest();
        //设置短信应用id
        req.setSmsSdkAppid(smsProperties.getAppId());
        //设置短信签名内容
        req.setSign(smsProperties.getSign());
        //设置模板id
        req.setTemplateID(smsProperties.getTemplateId());
        //手机号码
        String[] phoneNumbers = {SmsConstants.SMS_PHONE_PREFIX+phone};
        req.setPhoneNumberSet(phoneNumbers);
        //验证码
        String[] templateParams = {code};
        req.setTemplateParamSet(templateParams);
        try {
            SendSmsResponse res = smsClient.SendSms(req);
            log.info("短信发送成功,RequestId:{}",res.getRequestId());
        } catch (TencentCloudSDKException e) {
            log.error("短信发送失败,{}",e.getMessage());
        }
    }
}
